// pages/myAppointment/myAppointment.js
const APP = getApp()
const app = getApp()
const Utils = require('../../utils/uti.js')
Page({

  

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    // whether to disable join btn or not
    disableJoin: false,
    room:'',
    show10: false,
    name: '',
    pageNum: 1,
    pageSize: 4,
    yuyueList: [],
    statusType: []
  },


  
  tolook() {

    this.setData({ show10: true });
  },
  onClose10() {
    this.setData({ show10: false,
      room:""
    });
  },
  onChange10(e){
    this.setData({
      room: e.detail,
    });
    
  },
  // toRoom(){
  //   if (!this.data.room) {
  //     wx.showToast({
  //       title: '请提供一个有效的房间名',
  //       icon: 'none',
  //       duration: 2000
  //       }) 
  //     } else{
  //         var room = this.data.room
  //         this.setData({ show10: false });
  //          wx.navigateTo({
  //           url: '/pages/meeting/meeting?room=' + room,
  //       })
  //     }
 
  // },
  onChange(e) {
    this.setData({
      name: e.detail,
    });
    console.log(e.detail);

  },
  onSearch() {
    console.log('搜索' + this.data.name);
    this.setData({
      pageNum: 1,
      pageSize: 4,
      yuyueList: []
    })
    this.myYuYue()
  },
  onClick() {
    console.log('搜索' + this.data.name);
    this.setData({
      pageNum: 1,
      pageSize: 4,
      yuyueList: []
    })
    this.myYuYue()
  },


  // 再次预约
  toAppointmentReLaunch() {
    // APP.toAppointmentReLaunch()

    wx.reLaunch({
      url: '/pages/loginIndex/loginIndex',
    })
    // wx.navigateTo({
    //   url: '/pages/appointment/appointment',
    // })
  },
  // 取消预约
  deleteYuyue(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定删除吗',
      success(res) {
        if (res.confirm) {
          var subscribeId = e.currentTarget.dataset.subscribeid
          // /mobileUser/subscribe/remove/{subscribeId}// 逻辑删除预约   url: '/mobileUser/subscribe/remove/'+subscribeId,   get
          var postObj = {
            url: '/mobileUser/subscribe/remove/' + subscribeId,
            method: 'get',
          }
          APP.request.wxRequest(postObj).then(res => {
            if (res.data.code === 200) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000,
                success: () => {
                  that.setData({
                    pageNum: 1,
                    pageSize: 4,
                  })
                  that.onShow()
                }
              })
            }
          })



        } else if (res.cancel) {
          // wx.showToast({
          //   title: '删除取消',
          //   icon: 'none',
          //   duration: 1000
          // })
        }
      }
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //     //获取我的预约信息// /mobileUser/subscribe/selectSubscribes   get  拼接  pageNum:1,        pageSize:8
  myYuYue() {
    var postObj = {
      url: '/mobileUser/subscribe/selectSubscribes?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize + '&name=' + this.data.name,
      method: 'get',
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    APP.request.wxRequest(postObj).then(res => {
      wx.hideLoading()
      // console.log('第' + this.data.pageNum + '页', res.data.rows);
      var yuyueList = this.data.yuyueList
      var rows = res.data.rows
      var allArr = [...yuyueList, ...rows]
      allArr.forEach((items, i) => {
        var statusName = APP.selectDictLabel(this.data.statusType, items.status)
        items.statusName = statusName
        items.subscribeId=!!items.subscribeId ?items.subscribeId :"-"
        items.room = !!items.room ?items.room :"-"
        // console.log(statusName);
      })
      this.setData({
        yuyueList: allArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.channel = "";
    this.uid = Utils.getUid();
    this.lock = false;
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo
      });
    }


    this.setData({
      yuyueList: [],
      pageNum: 1,
      pageSize: 4,
    })

    APP.getFrontInfo().then(res => {})


    // 审核状态字典  字典
    APP.getDict('sys_subscribe_status').then(res => {
      console.log('审核状态字典', res)
      this.setData({
        statusType: res.data.data
      })
      // 获取第一页预约数组
      that.myYuYue()
      wx.stopPullDownRefresh()
    })



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    that.setData({
      pageNum: 1,
      pageSize: 4,
    })
    that.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    // 获取预约数组
    that.myYuYue()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onGotUserInfo: function (e) {
    let userInfo = e.detail.userInfo || {};
    // store data for next launch use
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    this.onJoin(userInfo);
  },
  /**
   * check if join is locked now, this is mainly to prevent from clicking join btn to start multiple new pages
   */
  checkJoinLock: function () {
    return !(this.lock || false);
  },

  lockJoin: function () {
    this.lock = true;
  },

  unlockJoin: function () {
    this.lock = false;
  },

  onJoin: function (userInfo) {
    userInfo = userInfo || {};
    let value = this.channel || "";

    let uid = this.uid;
    if (!value) {
      wx.showToast({
        title: '请提供一个有效的房间名',
        icon: 'none',
        duration: 2000
      })
    } else {
      var list = {
        url: '/mobileUser/subscribe/getVideoRoom/' + this.channel,
        method: 'get',

      }
      APP.request.wxRequest(list).then(res => {

        if (res.data.code === 200) {
          if (this.checkJoinLock()) {
            this.lockJoin();
            if (value === "agora") {
              // go to test page if channel name is agora
              wx.navigateTo({
                url: `../test/test`
              });
            } else if (value === "agora2") {
              // go to test page if channel name is agora
              wx.navigateTo({
                url: `../test2/test2`
              });
            } else {
              let role = "audience";
              role = "broadcaster";
              wx.navigateTo({
                url: `../meeting/meeting?channel=${value}&uid=${uid}&role=${role}`
              });
            }
          }
        }
      });

    }
  },
  onInputChannel: function (e) {
    let value = e.detail.value;
    this.channel = value;
  }

})